//
//  ChatPostViewController.m
//  MarryMe
//
//  Created by lanou3g on 16/1/13.
//  Copyright © 2016年 revocher. All rights reserved.
//

#import "ChatPostViewController.h"
#import <AVOSCloud.h>
#import <RongIMKit/RongIMKit.h>
#import <AVUser.h>
#import "RootViewController.h"
@interface ChatPostViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) IBOutlet UITableView *postTableView;
@property(strong,nonatomic) NSMutableArray * postArr;
@property(strong,nonatomic) NSMutableArray * friendArr;
@property(strong,nonatomic)AVObject *postObj;
@property(strong,nonatomic)AVObject *friendObj;

@end

@implementation ChatPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.postTableView.delegate = self;
    self.postTableView.dataSource = self;
    AVQuery *query = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
   self.postObj= [query getFirstObject];
    if (self.postObj==nil) {
      self.postObj  = [AVObject objectWithClassName:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
    }
    self.postArr  = [NSMutableArray array];
    if ([self.postObj objectForKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]]!=nil) {
     self.postArr   = [self.postObj objectForKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
    }

    AVQuery *query1 = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
    self.friendObj = [query1 getFirstObject];
    if (self.friendObj==nil) {
        self.friendObj = [AVObject objectWithClassName:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
    }
    self.friendArr  = [NSMutableArray array];
    if ([self.friendObj objectForKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]]!=nil) {
        self.friendArr  =[self.friendObj objectForKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
    }
    
    UIBarButtonItem *leftIrem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftIrem;

}
-(void)leftItemAction:(UIBarButtonItem *)sender{
    RootViewController *affVC = [[RootViewController alloc]init];
    affVC.selectedIndex = 2;
    [self.navigationController presentViewController:affVC animated: YES completion:nil];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.postArr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
      static NSString  *cell_id = @"ChatPostViewController";
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:cell_id];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cell_id];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"用户:%@  向您发送了好友请求~",self.postArr[indexPath.row]];
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提醒" message:@"是否同意该申请" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
       //己方好友列表加入操作
        [self.friendArr addObject:self.postArr[indexPath.row]];
        [self.friendObj  setObject:self.friendArr forKey:[NSString stringWithFormat:@"Friend%@",[AVUser currentUser].username]];
        [self.friendObj saveInBackground];
        //对方好友列表加入操作
        AVQuery *query = [AVQuery queryWithClassName:[NSString stringWithFormat:@"Friend%@",self.postArr[indexPath.row]]];
        AVObject *obj = [query getFirstObject];
        if (obj==nil) {
            obj = [AVObject objectWithClassName:[NSString stringWithFormat:@"Friend%@",self.postArr[indexPath.row]]];
        }
        NSMutableArray *arr = [NSMutableArray array];
        if ([obj objectForKey:@"FriendList"]!=nil) {
            arr = [obj objectForKey:@"FriendList"];
        }
        [arr addObject:[AVUser currentUser].username];
        [obj setObject:arr forKey:[NSString stringWithFormat:@"Friend%@",self.postArr[indexPath.row]]];
        [obj saveInBackground];
        //删除请求操作
        NSLog(@"%@********",self.postArr);
        [self.postArr removeObjectAtIndex:indexPath.row];
        NSLog(@"______%@",self.postArr);
        [self.postObj  setObject:self.postArr forKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
        [self.postObj saveInBackground];
        [self.postTableView reloadData];
       }];
       UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"拒绝" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //删除请求操作
        [self.postArr removeObjectAtIndex:indexPath.row];
        NSLog(@"______%@",self.postArr);
        [self.postObj  setObject:self.postArr forKey:[NSString stringWithFormat:@"Post%@",[AVUser currentUser].username]];
        [self.postObj saveInBackground];
        [self.postTableView reloadData];
    }];
    [alertVc addAction:okAction];
    [alertVc addAction:noAction];
    [alertVc addAction:cancelAction];
    [self.navigationController presentViewController:alertVc animated:YES completion:nil];


}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    



}


@end
